<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="医患沟通情况" >
            <div slot="left" style="display: flex; width: 100%; flex-wrap: wrap; ">
              <el-form-item  label="发⽣阶段" prop="eventPhaseArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.eventPhaseArr">
                    <el-checkbox v-for="(item, index) in eventPhaseList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.eventPhaseArr && models.eventPhaseArr.indexOf('147')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordNegotiation.eventPhaseOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%;" />
              <el-form-item  label="纠纷类型" prop="eventTypeArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.eventTypeArr">
                    <el-checkbox v-for="(item, index) in eventTypeList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.eventTypeArr && models.eventTypeArr.indexOf('157')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordNegotiation.eventTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%;" />
              <el-form-item  label="沟通事项" prop="eventNegotiationArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.eventNegotiationArr">
                    <el-checkbox v-for="(item, index) in eventNegotiationList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.eventNegotiationArr && models.eventNegotiationArr.indexOf('166')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordNegotiation.eventNegotiationOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="医患沟通情况">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">发⽣阶段</td>
                <td>{{ getEventPhases(models.eventPhaseArr) || '' }}</td>
                <td class="td-title">纠纷类型</td>
                <td>{{ getEventTypes(models.eventTypeArr) || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">沟通事项</td>
                <td colspan="3">{{ getEventNegotiations(models.eventNegotiationArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveDoctorAndPatientNegotiation } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureOneInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureOneInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'DoctorAndPatientNegotiation',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureOneInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {},
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      eventPhaseList: [
        { value: '140', label: '⼊院前（门急诊期间）' },
        { value: '141', label: '完善相关检查，诊断明确前' },
        { value: '142', label: '常规治疗过程中（如因等待检查、药物、⾎制品、会诊协调、病⼈对病情不理解等）' },
        { value: '143', label: '⼿术/有创操作术前谈话沟通阶段' },
        { value: '144', label: '术后观察、恢复阶段' },
        { value: '145', label: '出院前（如因不愿出院、出院带药等）' },
        { value: '146', label: '出院后' },
        { value: '147', label: '其他' }
      ],
      eventTypeList: [
        { value: '150', label: '医患沟通不良（患者对病情不理解）' },
        { value: '151', label: '医患⾔语冲突' },
        { value: '152', label: '医患肢体⾏为冲突' },
        { value: '153', label: '患者威胁/恐吓' },
        { value: '154', label: '历史遗留沟通问题（如病⼈在本院过往有纠纷、病⼈在他院过往有纠纷等）' },
        { value: '155', label: '社会问题（如三⽆病⼈、病⼈拖⽋医疗费⽤等）' },
        { value: '156', label: '患者单⽅⾯拖延出院' },
        { value: '157', label: '其他' }
      ],
      eventNegotiationList: [
        { value: '160', label: '诊疗效果' },
        { value: '161', label: '费⽤问题' },
        { value: '162', label: '就诊流程' },
        { value: '163', label: '医疗管理' },
        { value: '164', label: '相关政策' },
        { value: '165', label: '⽤药情况' },
        { value: '166', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        eventPhaseArr: (info.baqsjRecordNegotiation && info.baqsjRecordNegotiation.eventPhase) ? info.baqsjRecordNegotiation.eventPhase.split(',') : [],
        eventTypeArr: (info.baqsjRecordNegotiation && info.baqsjRecordNegotiation.eventType) ? info.baqsjRecordNegotiation.eventType.split(',') : [],
        eventNegotiationArr: (info.baqsjRecordNegotiation && info.baqsjRecordNegotiation.eventNegotiation) ? info.baqsjRecordNegotiation.eventNegotiation.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'DoctorAndPatientNegotiation',
        eventName: '医患沟通相关—医患沟通(纠纷)事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 事件差错详情 => 对象类型(不同的表单，定制不同)
        baqsjRecordNegotiation: {},
        // 事件差错详情 =>  (不同的表单，定制不同)
        eventPhaseArr: [],
        eventTypeArr: [],
        eventNegotiationArr: []
      }
      this.dialogVisible = true
    },
    getEventPhases(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.eventPhaseList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '147') {
            labels.push(`其他（${this.models.baqsjRecordNegotiation.eventPhaseOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getEventTypes(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.eventTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '157') {
            labels.push(`其他（${this.models.baqsjRecordNegotiation.eventTypeOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getEventNegotiations(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.eventNegotiationList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '166') {
            labels.push(`其他（${this.models.baqsjRecordNegotiation.eventNegotiationOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',');
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',');
          this.models.baqsjRecordNegotiation.eventPhase = this.models.eventPhaseArr.join(',');
          this.models.baqsjRecordNegotiation.eventType = this.models.eventTypeArr.join(',');
          this.models.baqsjRecordNegotiation.eventNegotiation = this.models.eventNegotiationArr.join(',');
          doSaveDoctorAndPatientNegotiation(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
